import java.util.PriorityQueue;

class MyTimerTask implements Comparable<MyTimerTask>{
    private Runnable runnable;
    private long time;

    public MyTimerTask(Runnable runnable, long time) {
        this.runnable = runnable;
        long curTime = System.currentTimeMillis();
        this.time = curTime + time;
    }

    public void run() {
        runnable.run();
    }

    public long getTime () {
        return time;
    }

    @Override
    public int compareTo(MyTimerTask o) {
        return (int)(this.time - o.time);
    }
}

class MyTimer {
    private PriorityQueue<MyTimerTask> queue = new PriorityQueue<>();
    Object locker = new Object();
    Thread t = null;

    public void schedule(Runnable runnable, long delay) {
        synchronized (locker) {
            MyTimerTask task = new MyTimerTask(runnable, delay);
            queue.offer(task);
            locker.notify();
        }
    }

    public MyTimer () {
        t = new Thread(()-> {
            synchronized (locker) {
                while(true) {
                    try {
                        if(queue.isEmpty()) {
                            locker.wait();
                        }
                        MyTimerTask task = queue.peek();
                        long curTime = System.currentTimeMillis();
                        if(curTime >= task.getTime()) {
                            queue.poll();
                            task.run();
                        } else {
                            locker.wait(task.getTime() - curTime);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        t.start();
    }
}

class MyBolckingQueue {
    private int[] elems;
    int head;
    int tail;
    int size;

    Object locker = new Object();

    public MyBolckingQueue(int capacity) {
        elems = new int[capacity];
        head = tail = 0;
        size = 0;
    }

    public void offer(int elem) throws InterruptedException {
        synchronized (locker) {
            while(size == elems.length) {
                //队列满了就阻塞
                locker.wait();
            }

            elems[tail] = elem;
            size ++;
            tail++;
            if(tail >= elems.length) {
                tail = 0;
            }
            locker.notify();
        }
    }

    public int take() throws InterruptedException {
        int elem;
        synchronized (locker) {
            while(size == 0) {
                //队列为空就阻塞等待
                locker.wait();
            }

            elem = elems[head];
            head++;
            size--;
            if(head == elems.length) {
                head = 0;
            }
            locker.notify();
        }

        return elem;
    }
}

public class Test {
    public static void main (String[] args){
        MyBolckingQueue queue = new MyBolckingQueue(100);

        //生产者模型
        Thread t1 = new Thread(()->{
            int i = 0;
           while(true) {
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               try {
                   queue.offer(i);
                   System.out.println("生产者t1生产元素"+i);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               i++;
           }
        });

        Thread t3 = new Thread(()->{
            int i = 999999999;
            while(true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                try {
                    queue.offer(i);
                    System.out.println("生产者t3生产元素"+i);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                i--;
            }
        });

        //消费者模型
        Thread t2 = new Thread(()->{
            while(true) {
                int ret = 0;
                try {
                    ret = queue.take();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("消费者t2消费元素"+ret);
            }
        });

        //消费者模型
        Thread t4 = new Thread(()->{
            while(true) {
                int ret = 0;
                try {
                    ret = queue.take();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("消费者t4消费元素"+ret);
            }
        });

        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }

    public static void main1(String[] args) {
        ;
        MyTimer timer = new MyTimer();
        timer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello 30000");
            }
        },10000);

        timer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello 2000");
            }
        },2000);

        timer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello 1000");
            }
        },1000);

        System.out.println("hello main");
    }
}
